/**
 *
 */
package com.angel.email.attachments.strategies.impl.multiple;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;

import com.angel.common.keyValue.KeyValueResult;
import com.angel.email.attachments.EmailAttachmentInfo;
import com.angel.email.attachments.strategies.AttachmentsStrategy;
import com.angel.email.constants.MimeTypePropertyConstants;
import com.angel.email.exceptions.EmailAccessException;
import com.angel.email.exceptions.EmailAttachmentException;
import com.angel.webapp.file.FileInformation;



/**
 * @author William
 * @since 14/August/2009
 *
 */
public class PlainTextsAttachmentsStrategy implements AttachmentsStrategy{

	public List<FileInformation> getAttachmentsFrom(DataHandler dataHandler, KeyValueResult simplesAttachmentsStrategies) throws EmailAttachmentException {
		List<FileInformation> filesInformation = new ArrayList<FileInformation>();
		Multipart multipart;
		try {
			multipart = (Multipart) dataHandler.getContent();
			Integer countParts = multipart.getCount();
			FileInformation fi = null;
			String contentType = null;
			for(int i = 0; i < countParts; i++){
				Part part = multipart.getBodyPart(i);
				String disposition = part.getDisposition();
				if(disposition != null){
					contentType = part.getContentType();
					if(contentType.indexOf(MimeTypePropertyConstants.TEXT_HTML_MIME_TYPE) > 0){
						InputStream content = part.getInputStream();
						fi = new EmailAttachmentInfo
						(
							part.getContentType(),
							part.getFileName(),
							content,
							0
						);
					}
				} else {
					contentType = part.getContentType();
					InputStream inputStream = null;

					if (part.isMimeType("multipart/alternative")) {
						Multipart mp2 = (Multipart) part.getContent();
						Part part2 = mp2.getBodyPart(0);
						inputStream = part2.getInputStream();
						fi = new EmailAttachmentInfo
							(
								part2.getContentType(),
								part2.getFileName(),
								inputStream,
								0
							);
					} else {
						inputStream = part.getInputStream();
						fi = new EmailAttachmentInfo
							(
								part.getContentType(),
								part.getFileName(),
								inputStream,
								0
							);
					}
				}
				filesInformation.add(fi);
			}
		} catch (IOException e) {
			throw new EmailAttachmentException("", e);
		} catch (MessagingException e) {
			throw new EmailAttachmentException("", e);
		}
		return filesInformation;
	}

	public FileInformation getAttachmentsFrom(Part part, KeyValueResult multipleAttachmentsStrategies) throws EmailAttachmentException {

		return null;
	}

	public FileInformation getAttachmentFrom(DataHandler dataHandler)
			throws EmailAccessException {
		// TODO Auto-generated method stub
		return null;
	}
}
